﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace QuickApp.DbInterface
{
    public abstract class QADbConnection
    {
        protected QADbConnection(QADbType type, string name)
        {
            this.Name = name;
            this.dbType = type;
        }

        private QADbType dbType;
        private string database;

        protected string connectionString;
        protected bool buildStringSuccess;
        public string Name { get; set; }
        public string Password { get; set; }
        public string UserId { get; set; }
        public string Database { get { return database; } set { database = value; buildStringSuccess = false; } }
        public string Server { get; set; }
        public bool TrustedConnection { get; set; }
        public string ConnectionString { set { connectionString = value; } }
        //So this can be read and used later to instantiate the correct type via the connection factory
        public QADbType DbType
        {
            get { return dbType; }
        }


        protected abstract bool buildConnString();
        public abstract DbConnection getNewConnection();



        protected bool isConnStringSet()
        {
            return !String.IsNullOrWhiteSpace(connectionString) && connectionString.Length > 5;
        }
        public DbConnection GetNewConnection()
        {
            if (buildStringSuccess || buildConnString())
                return getNewConnection();
            return null;
        }
        public override string ToString()
        {
            return Name;
        }
    }
    public enum QADbType : int
    {
        SqlServer = 1
    }
}
